﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using imaginecup.util;
using Imaginecup.gameState.malaria.component.factory;
using imaginecup.objectManager;
using Imaginecup.gameState.malaria.component;
using Imaginecup.observer;
using Imaginecup.gameState.malaria.component.panel.clickAble;
using Imaginecup.gameState.malaria.component.entity;
using Imaginecup.factory;
using Imaginecup.gameState.malaria.manager;



namespace Imaginecup.util.script.malaria
{
#if WINDOWS
    public class ScriptReader
    {
        private static ScriptReader scriptReader = null;
        private ScriptReader() { }
        private XmlNode m_RootNode;

        public static ScriptReader GetScriptReader()
        {
            if (scriptReader == null)
            {
                scriptReader = new ScriptReader();
            }
            return scriptReader;
        }

        public void LoadDocument(String filename)
        {
            XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(filename);

            m_RootNode = doc.DocumentElement;
        }


        public XmlNode GetDynamicData( String dynamicData )
        {
            return m_RootNode[dynamicData];
        }

        public int GetDataToInt(String dynamicData)
        {
            return int.Parse(GetDynamicData(dynamicData).InnerText);
        }

        public float GetDataToFloat(String dynamicData)
        {
            return float.Parse(GetDynamicData(dynamicData).InnerText);
        }

        public double GetDataToDouble(String dynamicData)
        {
            return double.Parse(GetDynamicData(dynamicData).InnerText);
        }


        public void ReadObjectData( ObjectManager objMan)
        {
            XmlNode ObjectNode = m_RootNode["ObjectList"];

            foreach (XmlNode childNode in ObjectNode.ChildNodes)
            {
                switch (childNode.Name)
                {
                    case "PanelList":
                        ReadPanelList(childNode, objMan);
                        break;
                    case "CharList":
                   //     ReadHomeList(childNode , objMan);
                        break;
                }
            }

        }

        public void ReadMalaria(MalariaGenerator malariaGenerator)
        {
            XmlNode ObjectNode = m_RootNode["ObjectList"];
            XmlNode CharNode = ObjectNode["MalariaList"];

            ReadMalaria(malariaGenerator, CharNode);
        }

        private void ReadMalaria(MalariaGenerator malariaGenerator, XmlNode CharNode)
        {
            foreach (XmlNode childNode in CharNode.ChildNodes)
            {
                float appearTime = ToFloat(childNode["출몰시간"].InnerText);
                float velocity = ToFloat(childNode["속도"].InnerText);

                ChildInfo info = new ChildInfo(appearTime, velocity);
                malariaGenerator.AddChildInfo(info);
            }
        }

        public void ReadCharInfo( ChildGenerator childGenrator )
        {
            XmlNode ObjectNode = m_RootNode["ObjectList"];
            XmlNode CharNode = ObjectNode["CharList"];

            ReadCharInfo(childGenrator , CharNode);
        }

        private void ReadCharInfo(ChildGenerator childGenerator , XmlNode CharNode)
        {
            foreach (XmlNode childNode in CharNode.ChildNodes)
            {
                float appearTime = ToFloat(childNode["출몰시간"].InnerText);
                float velocity = ToFloat( childNode["속도"].InnerText );
                
                ChildInfo info = new ChildInfo(appearTime , velocity);
                childGenerator.AddChildInfo(info);
            }
        }

        private void ReadPanelList(XmlNode panelRootNode, ObjectManager objMan)
        {

            CommunicationMan clickAbleCommunication = new CommunicationMan();
            foreach (XmlNode childNode in panelRootNode.ChildNodes)
            {
                switch (childNode.Name)
                {
                    case "Panel":
                        ReadPanel(childNode , objMan , clickAbleCommunication);
                        break;
                }
            }
            clickAbleCommunication.RegisterCommunicationWithRelease();
        }

        private void ReadPanel(XmlNode panelNode , ObjectManager objMan , CommunicationMan clickAbleCommunication)
        {
            ObjectFactory.GetObjectFactory().SetObjectMan(objMan);
            MALARIA_COMPONENT_ID id = ObjectFactory.GetObjectID(panelNode["객체타입"].InnerText);

            XmlNode objectInfoNode = panelNode["객체정보"];

            CmpClickAblePanel entity = (CmpClickAblePanel)ObjectFactory.GetObjectFactory().GetObject(id);
            EntityInfo entityInfo = GetEntityInfo(objectInfoNode);

            entity.SetEntityInfo(entityInfo);


            clickAbleCommunication.AddObserver(entity);
            clickAbleCommunication.AddSubject(entity);

            //switch( itemID ) 아이템 아이디에 따라 처리해야할 일있을경우 처리.(예외)
            //{
            //    case ItemID.ITEM_ID_PILL:
            //        break;
            //}
        }

        private EntityInfo GetEntityInfo(XmlNode entityNode)
        {
            EntityInfo entityInfo = new EntityInfo(ToInt(entityNode["X"].InnerText), ToInt(entityNode["Y"].InnerText),
                                                    ToInt(entityNode["SizeX"].InnerText), ToInt(entityNode["SizeY"].InnerText));
            return entityInfo;

        }     

        private float ToFloat(String data)
        {
            return float.Parse(data);
        }
        private int ToInt(String data)
        {
            return int.Parse(data);
        }

    }

#endif
}